import request from '@/utils/request'
import type { DeviceGroupVO, DeviceImageVO } from '@/types/circuitDiagram'

/**
 * 设备分组管理API
 */
export const deviceGroupApi = {
  /**
   * 获取所有设备分组（包含设备图片）
   */
  getAllGroupsWithImages() {
    return request<DeviceGroupVO[]>({
      url: '/device-group/all-with-images',
      method: 'get'
    })
  },

  /**
   * 获取所有设备分组
   */
  getAllGroups() {
    return request<DeviceGroupVO[]>({
      url: '/device-group/all',
      method: 'get'
    })
  },

  /**
   * 分页查询设备分组
   */
  getGroupPage(params: {
    page: number
    size: number
    groupName?: string
  }) {
    return request<{
      records: DeviceGroupVO[]
      total: number
      size: number
      current: number
    }>({
      url: '/device-group/page',
      method: 'get',
      params
    })
  },

  /**
   * 根据ID获取设备分组详情
   */
  getGroupDetail(id: String) {
    return request<DeviceGroupVO>({
      url: `/device-group/${id}`,
      method: 'get'
    })
  },

  /**
   * 创建设备分组
   */
  createGroup(data: {
    groupName: string
    sortOrder?: number
  }) {
    return request<DeviceGroupVO>({
      url: '/device-group',
      method: 'post',
      data
    })
  },

  /**
   * 更新设备分组
   */
  updateGroup(data: {
    id: string
    groupName: string
    sortOrder?: number
  }) {
    return request<DeviceGroupVO>({
      url: '/device-group',
      method: 'put',
      data
    })
  },

  /**
   * 删除设备分组
   */
  deleteGroup(id: String) {
    return request({
      url: `/device-group/${id}`,
      method: 'delete'
    })
  },

  /**
   * 批量删除设备分组
   */
  batchDeleteGroups(ids: String[]) {
    return request({
      url: '/device-group/batch',
      method: 'delete',
      data: { ids }
    })
  }
}

/**
 * 设备图片管理API
 */
export const deviceImageApi = {
  /**
   * 分页查询设备图片
   */
  getImagePage(params: {
    page: number
    size: number
    imageName?: string
    groupId?: string
  }) {
    return request<{
      records: DeviceImageVO[]
      total: number
      size: number
      current: number
    }>({
      url: '/device-image/page',
      method: 'get',
      params
    })
  },

  /**
   * 根据ID获取设备图片详情
   */
  getImageDetail(id: String) {
    return request<DeviceImageVO>({
      url: `/device-image/${id}`,
      method: 'get'
    })
  },

  /**
   * 上传设备图片
   */
  uploadImage(data: FormData) {
    return request<DeviceImageVO>({
      url: '/device-image/upload',
      method: 'post',
      data,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  /**
   * 更新设备图片信息
   */
  updateImage(data: {
    id: String
    imageName: string
    groupId: string
  }) {
    return request<DeviceImageVO>({
      url: '/device-image',
      method: 'put',
      data
    })
  },

  /**
   * 删除设备图片
   */
  deleteImage(id: String) {
    return request({
      url: `/device-image/${id}`,
      method: 'delete'
    })
  },

  /**
   * 批量删除设备图片
   */
  batchDeleteImages(ids: String[]) {
    return request({
      url: '/device-image/batch',
      method: 'delete',
      data: { ids }
    })
  },

  /**
   * 下载设备图片
   */
  downloadImage(id: String) {
    return request({
      url: `/device-image/${id}/download`,
      method: 'get',
      responseType: 'blob'
    })
  }
} 